import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';

var Util = require("../../utils/utils.js");
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    birth: '',
    show: false,
  },

  updateUserInfo() {
    let {
      id,
      nickname,
      city,
      email,
      password,
      gender,
      birth,
      avatar
    } = this.data;
    let user = {
      id,
      nickname,
      city,
      email,
      password,
      gender,
      birth: new Date(birth).toUTCString(),
      avatar
    }
    // console.log(user);
    let url = `${app.globalData.baseURL}/api/member/member/update`;
    wx.request({
      url,
      method: "POST",
      data: user,
      success(res) {
        // console.log(res);
        Toast("修改成功！");
      }
    });
  },

  onClickIconNickname() {
    Toast('昵称一个月内只能修改一次');
  },

  /**
   * 日期选择相关
   */
  onDisplay() {
    // console.log("ahha");
    this.setData({
      show: true
    });
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
  },
  onConfirm(event) {
    this.setData({
      show: false,
      birth: this.formatDate(event.detail),
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    let user = wx.getStorageSync('user');
    var that = this;
    wx.request({
      url: `${app.globalData.baseURL}/api/member/member/info/${user.id}`,
      success(res) {
        // console.log(res);
        let user = res.data.umsMember;
        // console.log(user);
        user.birth = Util.formatTime(new Date(user.birth));
        user.registerTime = Util.formatTime(new Date(user.registerTime));
        user.createTime = Util.formatTime(new Date(user.createTime));
        user.updateTime = Util.formatTime(new Date(user.updateTime));
        that.setData({
          ...user
        });
        // 刷新本地存储 user
        wx.setStorage({
          data: user,
          key: 'user',
        });
        // console.log(user);
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})